package com.missilike.biz.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.missilike.biz.IMilAccountContentService;
import com.missilike.biz.IMilContentCollectionService;
import com.missilike.domain.response.CollectionResponse;
import com.missilike.domain.response.MilUserFeeds;
import com.missilike.repository.entity.MilContentCollection;
import com.missilike.repository.entity.extend.MilCollectionExtend;
import com.missilike.repository.mapper.MilContentCollectionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author kin
 * @since 2019-01-17
 */
@Service
public class MilContentCollectionServiceImpl extends ServiceImpl<MilContentCollectionMapper, MilContentCollection> implements IMilContentCollectionService {

    private  IMilAccountContentService contentService;

    @Override
    public void setAccountContentService(IMilAccountContentService contentService) {
        this.contentService = contentService;
    }


    @Override
    public List<MilContentCollection> getCollectionList(Long id) {
        QueryWrapper<MilContentCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MilContentCollection::getContentId, id);

        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public CollectionResponse getUserBeCollectedList(String accountId, Integer pageSize, Long lastId, Integer type) {
        CollectionResponse response = new CollectionResponse();
        List<MilUserFeeds> feeds = contentService.getUserFeeds(accountId,lastId,pageSize,type);
        List<MilUserFeeds> collectedFeeds = new ArrayList<>();
        for (MilUserFeeds f : feeds){
            if(f.getCollectTimes() > 0) {
                collectedFeeds.add(f);
            }
        }
        collectedFeeds.sort(Comparator.comparing(MilUserFeeds::getCollectTimes).reversed());
        response.setTotal(baseMapper.selectCount(new QueryWrapper<MilContentCollection>().lambda().eq(MilContentCollection::getCollectionAccountId, accountId)));
        response.setUserFeeds(collectedFeeds);
        return response;
    }

    @Override
    public CollectionResponse getUserCollectionList (String accountId, Integer pageSize, Long lastId, Integer type) {
        CollectionResponse response = new CollectionResponse();
        List<MilCollectionExtend> collectionExtends = new ArrayList<>();
        response.setTotal(baseMapper.selectCount(new QueryWrapper<MilContentCollection>().lambda()
                .eq(MilContentCollection::getCollectionAccountId, accountId)));
        QueryWrapper<MilContentCollection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MilContentCollection::getCollectionAccountId, accountId);
        if (lastId != null && lastId > 0) {
            queryWrapper.lambda().lt(MilContentCollection::getId, lastId);
        }
        queryWrapper.lambda().orderByDesc(MilContentCollection::getCreateTime).last(" limit " + pageSize);
        List<MilContentCollection> collections = baseMapper.selectList(queryWrapper);
        for (MilContentCollection c : collections) {
            MilCollectionExtend collectionExtend = new MilCollectionExtend();
            BeanUtils.copyProperties(c, collectionExtend);
            collectionExtend.setContent(contentService.getContent(contentService.getById(c.getContentId())));
            collectionExtends.add(collectionExtend);
        }

        response.setCollections(collectionExtends);
        return response;
    }

    @Override
    public boolean add(String accountId, Long contentId, String collectionAccountId) {
        MilContentCollection contentCollection = new MilContentCollection();
        contentCollection.setAccountId(accountId);
        contentCollection.setContentId(contentId);
        contentCollection.setCollectionAccountId(collectionAccountId);
        contentCollection.setCreateTime(LocalDateTime.now());
        return this.save(contentCollection);
    }

    @Override
    public Integer getUserCollectionCount(String accountId) {
        return baseMapper.selectCount(new QueryWrapper<MilContentCollection>().lambda().eq(MilContentCollection::getAccountId, accountId));

    }

   @Override
    public Integer getUserBeCollectionCount(String accountId) {
        return baseMapper.selectCount(new QueryWrapper<MilContentCollection>().lambda().eq(MilContentCollection::getCollectionAccountId, accountId));

    }
}
